import $ from '../jquery-3.4.1.min.js';
import ajax from '../utils/ajax';


let buildingName;//定义楼栋
let homefloor;//定义楼层
let homeUnit;//定义单元


// 添加住户
$('#addOwner').hide()
$('#owner .owner-content-head span').click(function () {
    const spanText = $(this).text();
    if (spanText == '添加') {
        $('#main-right').children().hide();
        $('#addOwner').show();
        // 获取楼栋
        getBuilding()

        // 获取单元，层
        getHome();

    }
})

// 住户change的时候获取小区名称
$('#addOwner .addOwner-basic-items1 .ownerCommunity').change(function () {
    getBuilding()
})

// 获取楼栋
function getBuilding() {
    const community_id = $('#addOwner .addOwner-basic-items1 .ownerCommunity').val()
    ajax({
        url: '/building',
        type: 'GET',
        data: { community_id },
        success(res) {
            // console.log(res);
            buildingName = (res.result[0]).buildingName;
            // console.log(buildingName);
        }
    })
}

// 获取单元，层
function getHome() {
    ajax({
        url: '/building/getHome',
        type: 'GET',
        success(res) {
            console.log(res);
            homeUnit = (res.result[0]).homeUnit
            homefloor = (res.result[0]).homefloor
            defaultHomeNumber(res.result)
        }
    })
}

// 渲染房号
function defaultHomeNumber(data) {
    const html = data.map(item => (
        `
        <option value="${item.homeNumber}">${item.homeNumber}</option>
        `
    )).join('')
    $('#addOwner .ownerHomeNumber').html(html)
}

$('.addOwner-btn button').click(function () {
    const text = $(this).text();
    if (text == '保存') {
        const community_id = $('#addOwner .ownerCommunity').val()
        const ownerName = $('#addOwner .ownerName').val()
        const ownerIdentity = $('#addOwner .ownerIdentity').val()
        const ownerCheckTime = '2021-06-22'
        const ownerHomeNumber = $('#addOwner .ownerHomeNumber').val()
        const gender = $('[name=ownerGender]:checked').val()
        const ownerCardNumber = $('#addOwner .ownerCardNumber').val()
        const ownerPhone = $('#addOwner .ownerPhone').val()
        const ownerIdentityCard = $('#addOwner .ownerIdentityCard').val()
        console.log(
            community_id
            , ownerName
            , ownerIdentity
            , ownerCheckTime
            , ownerHomeNumber
            , gender
            , ownerCardNumber
            , ownerPhone
            , ownerIdentityCard
        );

        ajax({
            url: '/owner',
            type: 'POST',
            data: {
                community_id
                , ownerName
                , ownerIdentity
                , ownerCheckTime
                , ownerHomeNumber
                , gender
                , ownerCardNumber
                , ownerPhone
                , ownerIdentityCard
            },
            success(res) {
                if (res.code == 200) {
                    alert('业主新增成功');
                } else {
                    alert('业主新增失败');
                }


            }
        })
    }
})

$('#addOwner .addOwner-btn button').click(function () {
    $('#addOwner').hide();
    $('#owner').show();
})